/**
  * Scala编程实战 从集合中提取元素序列
  * P287
  */

object collection1018 extends App {

  val x = (1 to 10).toArray


  println(x.drop(3).toList) //List(4, 5, 6, 7, 8, 9, 10)
  println(x.dropWhile(_ < 6).toList) //List(6, 7, 8, 9, 10)
  println(x.dropRight(4).toList) //List(1, 2, 3, 4, 5, 6)

  println(x.take(3).toList) //List(1, 2, 3)
  println(x.takeWhile(_ < 3).toList) //List(1, 2)
  println(x.takeRight(3).toList) //List(8, 9, 10)

  println(x.slice(3, 5).toList) //List(4, 5)

  println(x.apply(3).toLong) //4

  println(x.head) //1
  println(x.headOption) //Some(1)
  println(x.init.toList) // List(1, 2, 3, 4, 5, 6, 7, 8, 9)
  println(x.last) //10
  println(x.lastOption) //Some(10)
  println(x.tail.toList) //List(2, 3, 4, 5, 6, 7, 8, 9, 10)
}

//https://www.iteblog.com/archives/1228.html
//Scala:fold,foldLeft和foldRight区别与联系

class Foo(val name: String, val age: Int, val sex: Symbol)

object Foo extends App {
  def apply(name: String, age: Int, sex: Symbol) = new Foo(name, age, sex)

  val numbers = List(5, 4, 8, 6, 2)
  numbers.fold(0) { (z, i) =>
    z + i
  }

  println("numbers==>" + numbers) //numbers==>List(5, 4, 8, 6, 2)

  val fooList = Foo("Hugh Jass", 25, 'male) ::
    Foo("Biggus Dickus", 43, 'male) ::
    Foo("Incontinentia Buttocks", 37, 'female) ::
    Nil

  val stringList = fooList.foldLeft(List[String]()) { (z, f) =>
    val title = f.sex match {
      case 'male => "Mr."
      case 'female => "Ms."
    }
    z :+ s"$title ${f.name}, ${f.age}"
  }

  println(stringList)


}

